﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01单例模式.饿汉式
{
    public class SingleHungry
    {
        private readonly static SingleHungry _singleHungry = new SingleHungry();
        private SingleHungry()
        {
        }
        public static SingleHungry GetSingleHungry()
        {
            return _singleHungry;
        }
    }
    public class SingleHungryTest
    {
        public static void FactTestHashCodeIsSame()
        {
            Console.WriteLine("单例模式.饿汉式测试!");
            //第一种测试： 构造函数私有的，new的时候报错
            //SingleHungry  _singleHungry=new SingleHungry();
            // 第二种测试： 比对创建多个对象，然后多个对象的Hashvalue
            var single1 = SingleHungry.GetSingleHungry();
            var single2 = SingleHungry.GetSingleHungry();
            var single3 = SingleHungry.GetSingleHungry();
            Console.WriteLine(single1.GetHashCode());
            Console.WriteLine(single2.GetHashCode());
            Console.WriteLine(single3.GetHashCode());
            //结论：饿汉式的单例模式不推荐使用，因为还没调用，对象就已经创建，造成资源的浪费;
        }
    }
}
